#!/usr/bin/env python
# encoding: utf-8
# @Time    : 2020/4/23 15:33
# @Author  : Mr. Cui
# @File    : views.py
# @Software: PyCharm
from flask import Blueprint, request, render_template, redirect, send_from_directory, abort
from .service import *
from base_server.base_check import *
import json

resources = Blueprint('resources', __name__,static_folder='static',template_folder='template')

re = Device_Manag()
pp = Cha_Project()
up = Xmind_Upload()
ta = Qa_addtask()

# 排期展示
@resources.route('/scheduling', methods=['get', 'post'])
# @check_permissions("/scheduling")
def scheduling():
    name = request.args.get('name')
    if name is None:
        return render_template('scheduling.html')
    else:
        pq = Scheduling()
        data = pq.date_rest()
        return render_template('scheduling_son.html', data=data)


@resources.errorhandler(404)
def page_not_found(e):
    return render_template('404.html')


@resources.errorhandler(500)
def internal_server_error(e):
    return render_template('500.html')


# 设备管理展示与新增
@resources.route('/adddevice', methods=['get', 'post'])
# @check_token
@check_permissions("/adddevice")
def addevice():
    if request.method == "POST":
        data = request.get_data()
        data = json.loads(data)
        devtype = data['devtype']
        devsystem = data['devsystem']
        devname = data['devname']
        name = data['name']
        version = data['version']
        devnotes = data['notes']
        re.appinsp(devname, name, devnotes, version, devtype, devsystem)
        return "ok"


# 设备管理展示
@resources.route('/device', methods=['get', 'post'])
# @check_permissions("/device")
def devices():
    alldata = re.appga()
    return render_template('device.html', alldata=alldata)


# 设备编辑后保存
@resources.route('/savedev', methods=['post', 'get'])
def savedev():
    if request.method == "POST":
        devid = request.form.get('devid')
        devname = request.form.get('devname')
        devst = request.form.get('devst')
        name = request.form.get('name')
        notes = request.form.get('notes')
        version = request.form.get('version')
        re.appeditp(devname, devst, name, notes, version, devid)
        return redirect("device")


# 修改使用状态
@resources.route('/usestatus', methods=['post', 'get'])
# @check_token
@check_permissions("/usestatus")
def usestatus():
    if request.method == 'POST':
        devuser = request.get_data()
        devuser = json.loads(devuser.decode("utf-8"))
        devid = devuser['devid']
        user = devuser['user']
        re.appusep(devid, user)
        return "ok"


# 查询手机类型
@resources.route('/typedev', methods=['post', 'get'])
def devtype():
    if request.method == 'POST':
        devtype = request.get_data()
        devtype = json.loads(devtype.decode("utf-8"))
        devtype = devtype['devtype']
        alldata = re.appga(devtype)
        return render_template('devtype.html', alldata=alldata)



# 卡车之家业务信息表
@resources.route('/project_information', methods=['post', 'get'])
# @check_permissions("/project_information")
def Project_information():
    pp_rturn = request.args.get('firstname')
    data = pp.cha(pp_rturn)
    return render_template('project_information.html', u=data)


# 卡车之家业务信息表-编辑
@resources.route('/project_information_edit', methods=['POST', 'GET'])
# @check_permissions("/project_information")
def Project_information_edit():
    if request.method == 'GET':
        id = request.args.get('id')
        data = pp.cha_only(id)
        print(data)
        return render_template('project_information_edit.html', u=data)

    else:
        id = request.form.get('id')
        Business = request.form.get('Business')
        Product = request.form.get('Product')
        PM = request.form.get('PM')
        Business_type = request.form.get('Business_type')
        DMP = request.form.get('DMP')
        QD_Dev = request.form.get('QD_Dev')
        HD_Dev = request.form.get('HD_Dev')
        DEV_Leader = request.form.get('DEV_Leader')
        qa = request.form.get('qa')
        Platform = request.form.get('Platform')

        pp.edit(id, Business, Product, PM, Business_type, DMP, QD_Dev, HD_Dev, DEV_Leader, qa, Platform)
        data = pp.cha_only(id)

        return redirect('project_information')


# 卡车之家业务信息表-新增
@resources.route('/project_information_added', methods=['POST', 'GET'])
def Project_information_added():
    if request.method == 'GET':
        return render_template('project_information_added.html')

    else:
        id = request.form.get('id')
        Business = request.form.get('Business')
        Product = request.form.get('Product')
        PM = request.form.get('PM')
        Business_type = request.form.get('Business_type')
        DMP = request.form.get('DMP')
        QD_Dev = request.form.get('QD_Dev')
        HD_Dev = request.form.get('HD_Dev')
        DEV_Leader = request.form.get('DEV_Leader')
        qa = request.form.get('qa')
        Platform = request.form.get('Platform')

        pp.added(Business, Product, PM, Business_type, DMP, QD_Dev, HD_Dev, DEV_Leader, qa, Platform)
        return redirect("project_information")



# xmind上传下载
@resources.route('/testcase', methods=['post', 'get'])
# @check_permissions('/testcase')
def upload():
    a = up.fileselect()
    if request.method == 'POST':
        f = request.files['file']
        up.fileupload(f)
        way = up.xmind_way()
        path = way + f.filename
        data = up.to_dict(path)
        print(data)
        up.fileinsert(f.filename, data)
        os.remove(path)
        return "1"
    else:
        return render_template('upload.html', a=a)


# 下载
@resources.route('/export_xls/<filename>', methods=['get'])
def return_file(filename):
    import os
    if request.method == "GET":
        search = up.xls_true(filename)
        if search:
            file_dir = os.path.join(up.xls_way(), filename)
        else:
            up.downexcel(filename)
            file_dir = os.path.join(up.xls_way(), filename)
        if os.path.isfile(file_dir):
            return send_from_directory(up.xls_way(), filename, as_attachment=True)
        abort(404)


# 查询
@resources.route('/selectfile', methods=['post', 'get'])
def select_file():
    if request.method == "POST":
        project = request.form.get('project')
        a = ""
        if (project == "全部"):
            a = up.fileselect()
        else:
            a = up.sel_file(project)
        return json.dumps(a)
#自动任务(查询)
@resources.route('/addtask', methods=['post', 'get'])
def add_task():
    data = ta.sel_task()
    return render_template('addtask.html',data = data)

#脚本上传
@resources.route('/taskupload', methods=['post', 'get'])
def task_upload():
    if request.method == 'POST':
        f = request.files['file']
        ta.fileupload(f)
        return "1"
    else:
        return render_template('addtask.html')

#添加脚本
@resources.route('/inserttask', methods=['post', 'get'])
def insert_task():
    if request.method == 'POST':
        name = request.form.get('name')
        cron = request.form.get('cron')
        ta.add_task(name,cron)
        return '1'
    else:
        return render_template('addtask.html')

#删除脚本
@resources.route('/deltask', methods=['post', 'get'])
def del_task():
    if request.method == 'POST':
        id = request.form.get('id')
        ta.del_task(id)
        return '1'
    else:
        return render_template('addtask.html')

#修改脚本
@resources.route('/edittask', methods=['post', 'get'])
def ed_task():
    if request.method == 'POST':
        id = request.form.get('id1')
        name = request.form.get('name1')
        cron = request.form.get('cron1')
        ta.edit_task(id,name,cron)
        return '1'
    else:
        return render_template('addtask.html')

# 启动、停止任务
@resources.route('/runstop', methods=['post', 'get'])
def run_stop():
    if request.method == "POST":
        status = request.form.get('status')
        task_cron = request.form.get('task_cron')
        id = request.form.get('id')
        ta.run(status,task_cron,id)
        return '1'